#__Author:Administrator
#__Date:2019/6/25
from src import models
loging_status = None

def loging():
    global loging_status
    user = input("UserName:").strip()
    pwd = input("Password:").strip()
    obj = models.Admin.login(user, pwd)
    if obj:
        print("欢迎%s" % user)
        loging_status = obj
    else:
        print("登录失败")


def create_school():
    name = input("输入学校名>>")
    city = input("城市>>")
    obj = models.School(name, city)
    obj.save()


def show_school():
    obj = models.School.get_all_list()
    for item in obj:
        print(item)

def create_teacher():
    name = input("讲师名>>")
    pwd = input("初始密码")
    level = int(input("讲师级别>>"))
    obj = models.Teacher(name, pwd, level)
    obj.save()
    print("【%s】讲级创建成功" % name)


def create_classes():
    obj = models.School.get_all_list()
    for k, v in enumerate(obj, 1):
        print(k, v)
    choice = int(input("请选择学校>>"))
    schoolNid = obj[choice - 1]
    name = input("输入班级名>>")
    #创建班级
    obj = models.Classes(name, schoolNid)
    #为班级分配讲师
    course_to_teacher_obj = models.Course_to_Teacher.get_all_list()
    if course_to_teacher_obj:
        for k, v in enumerate(course_to_teacher_obj, 1):
            print(k, v)
        choice = int(input("选择讲师"))
        try:
            course_to_teacher_Nid = course_to_teacher_obj[choice - 1]
            obj.create_teacher(course_to_teacher_Nid)
        except IndexError:
            print("选项不存在")
    obj.save()


def show_classes():
    #查看班级信息
    obj = models.Classes.get_all_list()
    for k, v in enumerate(obj, 1):
        print(k, v)
    choice = int(input("请选择班级>>"))
    try:
        classesNid = obj[choice - 1]
    except IndexError:
        print("选项不存在")
    #打印班级信息
    classesNid.show_classes_info

def create_course():
    obj = models.School.get_all_list()
    for k, v in enumerate(obj, 1):
        print(k, v)
    choice = int(input("请选择学校>>"))
    schoolNid = obj[choice - 1]
    name = input("输入课程名>>")
    price = input("输入课程价格>>")
    period = input("输入课程周期>>")
    obj = models.Course(name, price, period, schoolNid)
    obj.save()

def show_course():
    obj = models.Course.get_all_list()
    for item in obj:
        print("\033[34m %s \033[0m" % item)

def show_student():
    pass

def student_to_classes():
    #获取班级对象
    classes_obj = models.Classes.get_all_list()
    for k, v in enumerate(classes_obj, 1):
        print(k, v)
    choice = int(input("请选择班级>>"))
    try:
        classesNid = classes_obj[choice - 1]
    except IndexError:
        print("班级不存在")
    #获取学生对象
    while True:
        students_obj = models.Student.get_all_list()
        for k, v in enumerate(students_obj, 1):
            print(k, v)
        choice = int(input("请选择学生>>"))
        try:
            studentsNid = students_obj[choice - 1]
            studentsNid.allot_classes = classesNid
            classesNid.create_students(studentsNid)
            choice = input("是否继续添加学生【y/n】>>")
            if choice.lower() == "y":
                continue
            else:
                break
        except IndexError:
            print("选项不存在")
    studentsNid.save()
    classesNid.save()

def course_to_teacher():
    course_obj = models.Course.get_all_list()
    for k, v in enumerate(course_obj, 1):
        print(k, v)
    choice = int(input("请选择课程>>"))
    try:
        couresNid = course_obj[choice - 1]
    except IndexError:
        print("选项不存在")
    teacher_obj = models.Teacher.get_all_list()
    for k, v in enumerate(teacher_obj, 1):
        print(k, v)
    choice = int(input("请选择讲师>>"))
    try:
        teacherNid = teacher_obj[choice - 1]
    except IndexError:
        print("选项不存在")
    obj = models.Course_to_Teacher.get_all_list()
    for item in obj:
        if item.teacherID.teacherName == teacherNid.teacherName and item.courseID.courseName == couresNid.courseName:
            print("已经存在")
            break
    else:
        obj = models.Course_to_Teacher(couresNid, teacherNid)
        obj.save()
        print("【%s】创建成功" % obj)

def show_choice():
    func_list = """
    1.登录
    2.创建讲师
    3.创建班级
    4.创建课程
    5.查看课程
    6.创建讲师关联课程关系
    7.创建班级关联学生关系
    8.创建学校
    9.查看学校
    10.查看班级信息
    """
    print("\033[33m %s \033[0m" % func_list)

def main():
    dist = {
        "1": loging,
        "2": create_teacher,
        "3": create_classes,
        "4": create_course,
        "5": show_course,
        "6": course_to_teacher,
        "7": student_to_classes,
        "8": create_school,
        "9": show_school,
        "10": show_classes
    }

    while True:
        show_choice()
        choice = str(input("请输入选项>>"))
        if choice not in dist:
            raise Exception("选择不存在，请选择%s" % show_choice())

       # if not loging_status and choice != "1":
        #    loging()
        dist[choice]()


